fetch('/r3')
.then(response => response.json())
.then(data => {
  // 检查并清理数据
  const wordCloudData = data.wordcloud_data.filter(d => !isNaN(d.value) && d.value >= 0)
                                           .map(d => ({ name: d.name, value: d.value }));

  // 创建 echarts 实例并渲染词云图
  const chart = echarts.init(document.getElementById('r3'));
  const options = {
    series: [{
      type: 'wordCloud',
      shape: 'circle',
      left: 'center',
      top: 'center',
      width: '80%',
      height: '80%',
      right: null,
      bottom: null,
      sizeRange: [12, 60],
      rotationRange: [-90, 90],
      rotationStep: 45,
      gridSize: 8,
      drawOutOfBound: false,
      textStyle: {
        normal: {
          fontFamily: 'sans-serif',
          fontWeight: 'bold',
          color: function () {
            return 'rgb(' + [
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160),
              Math.round(Math.random() * 160)
            ].join(',') + ')';
          }
        }
      },
      data: wordCloudData
    }]
  };
  chart.setOption(options);
});